package cn.wolfcode.elasticjob.config;

import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * zookeeper 注册中心配置类
 */
@Configuration
public class RegistryCenterConfig {

    ////启动作业配置
    @Bean(initMethod = "init")  //交给spring完成bean的创建，创建完成之后调用初始化的方法
    public CoordinatorRegistryCenter createRegistryCenter(
            @Value("${elastic-job.zookeeper-url}") String zookeeperUrl, @Value("${elastic-job.group-name}") String groupName) {
        //参数1：配置zookeeper的地址,调度任务的组名；参数2：项目名即可
        ZookeeperConfiguration zookeeperConfiguration =
                new ZookeeperConfiguration(zookeeperUrl, groupName);
        //设置超时时间（一定要配置）
        zookeeperConfiguration.setSessionTimeoutMilliseconds(100); //其实这个100也可以配到配置文件中
        //创建注册中心配置
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
        //初始化
        //regCenter.init();     //在 @Bean 中配置了调用初始化的方法，所以这里不需要了
        return regCenter;
    }

}
